﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
 

namespace TracNghiemTinHocDaiCuong.Common
{
    public class Methods
    {
        public static int ConvertNumber(string exp)
        {
            int number;
            return int.TryParse(exp, new NumberStyles(), CultureInfo.CurrentCulture, 
                out number) ? number : 0;
        }

        public static float ConvertFloating(string exp)
        {
            float number;
            return float.TryParse(exp, new NumberStyles(), CultureInfo.CurrentCulture, 
                out number) ? number : 0;
        }

        public static double ConvertDouble(string exp)
        {
            double number;
            return double.TryParse(exp, new NumberStyles(), CultureInfo.CurrentCulture, 
                out number) ? number : 0;
        }

        public static string ConvertString(string exp)
        {
            return !string.IsNullOrEmpty(exp) ? exp : "";
        }

        public static bool ConvertBoolean(string exp)
        {
            bool flag;
            return bool.TryParse(exp, out flag) ? flag : false;
        }

        public static DateTime ConvertDateTime(string exp)
        {
            DateTime dt;
            return DateTime.TryParse(exp, CultureInfo.CurrentCulture, 
                new DateTimeStyles(), out dt) ? dt : DateTime.MinValue;
        }

        public static int ConvertDateTimeToInt(string exp)
        {
            if (string.IsNullOrEmpty(exp))
                return -1;
            string[] time = exp.Split(':');
            if (ConvertNumber(time[0]) > 23)
                return -1;
            if (ConvertNumber(time[1]) > 59)
                return -1;
            return ConvertNumber(time[0]) * 100 + ConvertNumber(time[1]);
        }

        public static string ConvertDateTimeToString(string exp)
        {
            if (string.IsNullOrEmpty(exp))
                return "00000000";
            string[] time = exp.Split(':');
            if (ConvertNumber(time[0]) > 23)
                return "00000000";
            if (ConvertNumber(time[1]) > 59)
                return "00000000";
            return string.Format(CultureInfo.CurrentCulture, "{0}{1}", 
                time[0], time[1]);
        }
    }
}